package com.itdianzi.controller.system;

import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.controller.produce.MQProducer;
import com.itdianzi.domain.system.Dept;
import com.itdianzi.domain.system.Role;
import com.itdianzi.domain.system.User;
import com.itdianzi.service.system.DeptService;
import com.itdianzi.service.system.RoleService;
import com.itdianzi.service.system.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private MQProducer mqProducer;


    @RequestMapping(value = "/list",name = "用户管理查询")
    public String list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer size) {
        PageInfo pageInfo = userService.findAll(page, size,super.companyId);
        request.setAttribute("page",pageInfo);
        return "/system/user/user-list";
    }
    @Autowired
    private DeptService deptService;
    @RequestMapping(value = "/toAdd",name = "用户新建")
    public String toAdd() {

        List<Dept> deptList = deptService.findAll(super.companyId);
        request.setAttribute("deptList",deptList);

        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit",name = "用户添加或修改")
    public String edit(User user) {
        user.setCompanyId(companyId);
        user.setCompanyName(companyName);

        if (StringUtils.isBlank(user.getId())) {
            //保存数据
            userService.save(user);

            //发送文件
            String to = user.getEmail();
            String subject = "恭喜您，加入saas云平台";
            String content = "您的用户名是：" + user.getEmail() + ",您的密码是：" + user.getPassword();
            //放入map
            Map map = new HashMap();
            map.put("to", to);
            map.put("subject", subject);
            map.put("content", content);
            mqProducer.sendDataToQueue("mail.send", map);

        } else {
            userService.update(user);
        }
        return "redirect:/system/user/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "跳转到修改页面")
    public String toUpdate(String id) {
        User user= userService.findById(id);
        request.setAttribute("user",user);

        List<Dept> deptList = deptService.findAll(companyId);
        request.setAttribute("deptList",deptList);
        return "/system/user/user-update";

    }

    @RequestMapping(value = "/delete",name = "删除用户")
    public String delete(String id) {
        userService.delete(id);
        return "redirect:/system/user/list.do";
    }
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/roleList",name = "用户分配角色的页面")
    public String roleList(String id) {
        //查询当前修改角色的用户信息
        User user = userService.findById(id);
        request.setAttribute("user",user);
        //查询所有角色的信息
        List<Role> roleList = roleService.findAll(companyId);
        request.setAttribute("roleList", roleList);
        //查询当前用户的角色信息
        List<Role> userRoles = roleService.findByUserId(id);
        String userRoleStr = "";
        //组装一个用户所具有的角色信息id字符串
        for (Role userRole : userRoles) {
            userRoleStr += userRole.getId() + ",";
        }
        request.setAttribute("userRoleStr", userRoleStr);
        return "/system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name = "修改用户的角色信息")
    public String changeRole(String userid,String roleIds) {

        userService.updateUserRole(userid, roleIds);

        return "redirect:/system/user/list.do";
    }
}
